//----------------------------------------------------------------------------
//  ,-.
// /_\___ ,   Ninja Framework
// '  ___\    (C) Riccardo Capra - http://riccar.do
//  //   \\
//----------------------------------------------------------------------------
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------

#include "NjExample01.h"

#if NJ_PLATFORM_OS_LINUX
#	include "NjHighResolutionTimer.h"

	//----------------------------------------------------------------------------
	class NjHighResolutionTimer::Static
	{
	public:
		static const TimeTick ONE_S_OVER_ONE_NS=1000000000;
		
	private:
		NJ_DISABLE_INSTANCE(Static);
	};
	//----------------------------------------------------------------------------
	void NjHighResolutionTimer::Initialize()
	{
		if (ms_uTickFrequency>0)
			return;

		timespec kTime;
		
		NjInt const iResult(clock_getres(CLOCK_REALTIME, &kTime));
		TimeTick uPeriodNs;

		if (iResult==0)
			uPeriodNs=TimeTick(kTime.tv_sec)*Static::ONE_S_OVER_ONE_NS+kTime.tv_nsec;
		else
		{
			uPeriodNs=Static::ONE_S_OVER_ONE_NS;
			NJ_BREAK();
		}

		ms_uTickFrequency=Static::ONE_S_OVER_ONE_NS/uPeriodNs;
		ms_fTickPeriod=NjFloat(uPeriodNs)/NjFloat(Static::ONE_S_OVER_ONE_NS);
	}
	//----------------------------------------------------------------------------
	NjHighResolutionTimer::TimeTick NjHighResolutionTimer::GetTick()
	{
		timespec kTime;
		
		NjInt const iResult(clock_gettime(CLOCK_REALTIME, &kTime));

		if (iResult!=0)
		{
			NJ_BREAK();
			return 0;
		}

		return TimeTick(kTime.tv_sec)*Static::ONE_S_OVER_ONE_NS+kTime.tv_nsec;
	}
	//----------------------------------------------------------------------------
#endif // NJ_PLATFORM_OS_LINUX
